package com.gxa.service.resume.impl;

import com.gxa.common.R.ResponseData;
import com.gxa.mapper.resume.MianShiMapper;
import com.gxa.pojo.Jianli;
import com.gxa.service.resume.MianShiService;
import com.gxa.utils.EmployeeAge;
import com.gxa.utils.JwtUtils;
import com.gxa.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MianShiServiceImpl implements MianShiService {
    @Autowired(required = false)
    private MianShiMapper mianShiMapper;

    @Autowired(required = false)
    private JianLiServiceImpl jianLiService;

    /*
    登录方法
     */
    @Override
    public ResponseData login(String phone, String password) {
        Jianli jianli = mianShiMapper.login(phone);
        String newPassword = MD5Utils.encrypt(password, MD5Utils.MD5_KEY);
        System.out.println(newPassword);
        ResponseData responseData = new ResponseData();
        responseData.setCode(500);
        responseData.setMsg("登录名或密码错误");
        if (jianli!=null){
            if (jianli.getPassword().equals(newPassword)){
                //创建token
                String token = JwtUtils.createJwt(jianli.getId() + "",
                                                        JwtUtils.JWT_SECERT, phone,
                                                        JwtUtils.TTMILLIS);

                responseData.setCode(200);
                responseData.setMsg("恭喜您，登录成功");
                responseData.setData(jianli);
                responseData.setToken(token);
            }
        }
        return responseData;
    }

    @Override
    public ResponseData register(String phone, String password, String name,int sex, String educated, String height) {
        Jianli jianli = mianShiMapper.login(phone);
        ResponseData responseData = new ResponseData();
        if (jianli!=null){
            responseData.setCode(500);
            responseData.setMsg("该账户已存在");
            return responseData;
        }
        mianShiMapper.register(phone,password,name,sex,educated,height);
        responseData.setCode(200);
        responseData.setMsg("注册成功");
        return responseData;
    }

    @Override
    public ResponseData updatePassword(String phone, String password,String newPassword) {
        ResponseData responseData = new ResponseData();
        Jianli jianli = mianShiMapper.login(phone);
        if (!jianli.getPassword().equals(password)) {
            responseData.setCode(500);
            responseData.setMsg("原密码密码错误");
            return responseData;
        }
        mianShiMapper.updatePassword((int) jianli.getId(), newPassword);
        responseData.setCode(200);
        responseData.setMsg("修改密码成功");
        return responseData;
    }
    /*
    修改简历信息
     */
    @Override
    public ResponseData updateJianLi(Jianli jianli) {
        jianli.setStatus(0);
        if (jianli.getBirthday()!=null
            && jianli.getJiGuan()!=null
            && jianli.getMinZu()!=null
            && jianli.getJiGuan()!=null
            && jianli.getPosition()!=null
            && jianli.getXinChou()!=null){
            jianli.setStatus(1);
        }
        if (jianli.getBirthday()!=null) {
            int employeeAge = EmployeeAge.getEmployeeAge(jianli.getBirthday());
            jianli.setAge(employeeAge);
        }
        mianShiMapper.updateJianLi(jianli);
        ResponseData responseData = new ResponseData();
        responseData.setCode(200);
        responseData.setMsg("修改成功");
        return responseData;
    }
}
